package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.TravelContract;
import com.eling.elcms.travel.model.TravelContractCharge;
import com.eling.elcms.travel.service.ITravelContractChargeManager;
import com.eling.elcms.travel.service.ITravelContractManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TravelContractChargeController extends BaseFormController{
	@Autowired
	private ITravelContractChargeManager travelContractChargeManager;
	@Autowired
	private ITravelContractManager travelContractManager;
	
	@RequestMapping("/api/travelcontractcharge/save")
    @ResponseBody
	public TravelContractCharge save(@ModelAttribute@ModelFilling TravelContractCharge travelcontractcharge){
		return travelContractChargeManager.save(travelcontractcharge);
	}
	
	@RequestMapping("/api/travelcontractcharge/savecontract")
    @ResponseBody
	public TravelContractCharge saveContract(@ModelAttribute@ModelFilling TravelContractCharge travelcontractcharge){
		travelcontractcharge.getTravelContract().setStatus(TravelContract.Status.Valid); 
		TravelContract tc=travelContractManager.save(travelcontractcharge.getTravelContract());
		travelcontractcharge.setTravelContract(tc);
		return travelContractChargeManager.save(travelcontractcharge);
	}
	@RequestMapping("/api/travelcontractcharge/query")
    @ResponseBody
	public List<TravelContractCharge> query(@ModelAttribute TravelContractCharge travelcontractcharge){
		return travelContractChargeManager.query(travelcontractcharge);
	}
	
	@RequestMapping("/api/travelcontractcharge/search")
	@ResponseBody
	public List<TravelContractCharge> search(@ModelAttribute SearchCondition sc,@ModelAttribute TravelContractCharge travelcontractcharge){
		sc.setQueryCondition(travelcontractcharge);
		return travelContractChargeManager.search(sc);
	}
	
	@RequestMapping("/api/travelcontractcharge/{pkTravelContractCharge}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkTravelContractCharge){
		travelContractChargeManager.remove(pkTravelContractCharge);
		return new Msg("删除成功");
	}
}
